home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / xml4j.jar / com / ibm / xml / parser / Stderr.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-08-30  |  4.2 KB  |  164 lines

  1. package com.ibm.xml.parser;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.io.Reader;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.util.Hashtable;
  11. import java.util.Stack;
  12. import java.util.StringTokenizer;
  13.  
  14. public class Stderr implements ErrorListener, StreamProducer {
  15.    private static final boolean DEBUG_ENTITY_RESOLUTION = false;
  16.    public static PrintWriter printer;
  17.    protected String name;
  18.    protected URL url;
  19.    protected Stack stack = new Stack();
  20.    protected Hashtable catalog;
  21.    protected boolean isPrintWarning = true;
  22.    protected String publicId;
  23.    protected String systemId;
  24.  
  25.    public Stderr(String var1) {
  26.       this.name = var1;
  27.  
  28.       try {
  29.          this.url = new URL(this.name);
  30.          this.systemId = this.url.toString();
  31.       } catch (MalformedURLException var5) {
  32.          try {
  33.             this.url = file2URL(var1);
  34.             this.systemId = this.url.toString();
  35.          } catch (MalformedURLException var3) {
  36.             throw new LibraryException("Stderr#Stderr(): Internal Error: " + var5);
  37.          } catch (SecurityException var4) {
  38.             printer.println("Specify a complete URL");
  39.          }
  40.       }
  41.    }
  42.  
  43.    public void setPrintWarning(boolean var1) {
  44.       this.isPrintWarning = var1;
  45.    }
  46.  
  47.    public static URL file2URL(String var0) throws MalformedURLException {
  48.       var0 = var0.replace(File.separatorChar, '/');
  49.       String var1 = System.getProperty("user.dir").replace(File.separatorChar, '/') + "/";
  50.       if (var1.charAt(0) != '/') {
  51.          var1 = "/" + var1;
  52.       }
  53.  
  54.       URL var2 = new URL("file", "", var1);
  55.       if (var0.length() >= 2 && var0.charAt(1) == ':') {
  56.          char var3 = Character.toUpperCase(var0.charAt(0));
  57.          if (var3 >= 'A' && var3 <= 'Z') {
  58.             var0 = "file:///" + var0;
  59.          }
  60.       }
  61.  
  62.       return new URL(var2, var0);
  63.    }
  64.  
  65.    public int error(String var1, int var2, int var3, Object var4, String var5) {
  66.       if (var1 == null) {
  67.          var1 = this.name;
  68.       }
  69.  
  70.       if (var3 > 0) {
  71.          --var3;
  72.       }
  73.  
  74.       if (this.isPrintWarning || !(var4 instanceof String) || !((String)var4).startsWith("W_")) {
  75.          printer.println(var1 + ": " + var2 + ", " + var3 + ": " + var5);
  76.       }
  77.  
  78.       printer.flush();
  79.       return 1;
  80.    }
  81.  
  82.    private String URI2URL(String var1) throws IOException {
  83.       if (var1.length() > 4 && var1.substring(0, 4).equalsIgnoreCase("urn:")) {
  84.          String var2 = Util.normalizeURN(var1);
  85.          if (this.catalog == null || !this.catalog.containsKey(var2)) {
  86.             throw new IOException("Can't resolve URN: " + var1);
  87.          }
  88.  
  89.          var1 = (String)this.catalog.get(var2);
  90.       }
  91.  
  92.       return var1;
  93.    }
  94.  
  95.    public Source getInputStream(String var1, String var2, String var3) throws IOException {
  96.       this.publicId = var2;
  97.       if (var2 != null && this.catalog != null && this.catalog.containsKey(var2)) {
  98.          String var4 = (String)this.catalog.get(var2);
  99.          var4 = this.URI2URL(var4);
  100.          URL var5 = new URL(this.url, var4);
  101.  
  102.          try {
  103.             Source var6 = new Source(var5.openStream());
  104.             this.stack.push(this.url);
  105.             this.url = var5;
  106.             this.systemId = var4;
  107.             return var6;
  108.          } catch (IOException var7) {
  109.          }
  110.       }
  111.  
  112.       var3 = this.URI2URL(var3);
  113.       URL var10 = new URL(this.url, var3);
  114.       this.stack.push(this.url);
  115.       this.url = var10;
  116.       this.systemId = var3;
  117.       return new Source(var10.openStream());
  118.    }
  119.  
  120.    public void closeInputStream(Source var1) {
  121.       if (!this.stack.empty()) {
  122.          this.url = (URL)this.stack.pop();
  123.       }
  124.  
  125.    }
  126.  
  127.    public void loadCatalog(Reader var1) throws IOException {
  128.       if (this.catalog == null) {
  129.          this.catalog = new Hashtable();
  130.       }
  131.  
  132.       BufferedReader var2 = new BufferedReader(var1);
  133.  
  134.       String var3;
  135.       while((var3 = var2.readLine()) != null) {
  136.          StringTokenizer var4 = new StringTokenizer(var3, "\t");
  137.          if (var4.hasMoreTokens()) {
  138.             String var5 = var4.nextToken();
  139.             if (var4.hasMoreTokens()) {
  140.                String var6 = var4.nextToken();
  141.                if (var5.length() > 4 && var5.substring(0, 4).equalsIgnoreCase("urn:")) {
  142.                   this.catalog.put(Util.normalizeURN(var5), var6);
  143.                } else {
  144.                   this.catalog.put(var5, var6);
  145.                }
  146.             }
  147.          }
  148.       }
  149.  
  150.    }
  151.  
  152.    public String getPublicId() {
  153.       return this.publicId;
  154.    }
  155.  
  156.    public String getSystemId() {
  157.       return this.systemId;
  158.    }
  159.  
  160.    static {
  161.       printer = new PrintWriter(System.err);
  162.    }
  163. }
  164.